# -*- coding: utf-8 -*-
'''
@文件    :log.py
@说明    :
@时间    :2025/08/28 15:30:00
@作者    :浪晋
@版本    :1.0
'''
from typing import Optional, Tuple, List, Dict
from tortoise.expressions import Q
from fastapi.exceptions import HTTPException
from app.cruds.monitor.log import crud, Total
from app.schemas.monitor.log import QuerySchema, CreateSchema, UpdateSchema

class Service:

    async def list(self, payload: QuerySchema) -> Tuple[Total, List[Dict]]:
        search = Q()
        order = ["-create_time"]
        total, objs = await crud.list(page_num=payload.pageNum, page_size=payload.pageSize, search=search, order=order)
        result = []
        for obj in objs:
            item = obj.to_dict(exclude=['payload','result','error_log','headers'])
            result.append(item)
        return total, result

    async def create(self, data: CreateSchema) -> Dict:
        obj =  await crud.create(data)
        return obj.to_dict()

    async def get(self, id: int):
        obj = await crud.get_by_id(id)
        if not obj:
            raise HTTPException(status_code=200, detail="数据不存在")
        return obj.to_dict()

service = Service()